package com.example.springbootquartz.job;

import com.example.springbootquartz.dao.JobTestDao;
import com.example.springbootquartz.model.JobTest;
import com.example.springbootquartz.tools.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Component
public class MyJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context)
            throws JobExecutionException {
        JobTestDao jobTestDao = SpringUtils.getBean(JobTestDao.class);

        Date date = new Date();
        String string = ">>>>>>>>>>>>>>>>>>>>>>>>>> " + this.getClass().getName()
                + "执行了。。。。。。。" + context.getTrigger().getKey().getName()
                + " >>>>>>>>>>> 时间为：" + date;
        JobTest jobTest = new JobTest();
        jobTest.setId(UUID.randomUUID().toString());
        jobTest.setMessage(string);
        jobTest.setCreateTime(new Timestamp(date.getTime()));
        jobTestDao.insertTest(jobTest);
        log.info(string);

        //值和"jobDetailFactoryBean.setApplicationContextJobDataKey("abc");"对应
//        ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail()
//                .getJobDataMap().get("abc");

    }
}
